#!/bin/sh

#判断build文件夹是否存在，存在则删除
if [ -d "${SRCROOT}/build" ]
then
rm -rf "${SRCROOT}/build"
fi

# 替换项目中.framework/Headers/项目名-Swift.h里面的内容修改
change_Framew_header_Swift_File_Content() {
	SWIFT_FILE="$1"
	if [ -e "$SWIFT_FILE" ]
	then
		echo "替换项目中.framework/Headers/项目名-Swift.h里面的内容修改"
		str1="#if 0"
		str2="#elif defined(__arm64__) && __arm64__"
		str3="#if defined(__arm64__) && __arm64__ || (__x86_64__) && x86_64"
		# 修改
		#查找str1替换成空
		sed -i "" "s/${str1}//g" $SWIFT_FILE
		sed -i "" "s/${str2}//g" $SWIFT_FILE
		sed -i "" "1a\\"$'\n'"${str3}" $SWIFT_FILE
	fi
}

# 2022-10-10 不再支持构建 armv7、armv7s 以及 i386 架构的 iOS 项目。
# 模拟器版本直接包含arm64、x86
check_xcodebuild_version_14_or_later() {
	XCODE_VERSION_INFO=`xcodebuild -version`
	version=${XCODE_VERSION_INFO:6:2}
	if [[ "$version" < "14" ]]
	then
		echo false
	else
		echo true
	fi
}
xcodebuild_version_14_or_later=`check_xcodebuild_version_14_or_later`

#要build的target名
TARGET_NAME="${TARGETNAME%-*}"
if [[ $1 ]]
then
TARGET_NAME=$1
fi

UNIVERSAL_OUTPUT_FOLDER="${SRCROOT}/${PROJECT_NAME}/"
IPHONESIMULATOR_FRAMEWORK_DIR="${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${TARGET_NAME}.framework"
IPHONE_FRAMEWORK_DIR="${BUILD_DIR}/${CONFIGURATION}-iphoneos/${TARGET_NAME}.framework"

#创建输出目录，并删除之前的framework文件
mkdir -p "${UNIVERSAL_OUTPUT_FOLDER}"
rm -rf "${UNIVERSAL_OUTPUT_FOLDER}/${TARGET_NAME}.framework"

if [ !xcodebuild_version_14_or_later ]
then
	xcodebuild -target "${TARGET_NAME}" ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build
fi
xcodebuild -target "${TARGET_NAME}" ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphonesimulator BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build

#拷贝framework到univer目录
# cp -R "${IPHONESIMULATOR_FRAMEWORK_DIR}" "${UNIVERSAL_OUTPUT_FOLDER}"

#合并framework，输出最终的framework到build目录
# if [ !xcodebuild_version_14_or_later ]
# then 
# 	lipo -create -output "${UNIVERSAL_OUTPUT_FOLDER}/${TARGET_NAME}.framework/${TARGET_NAME}" "${IPHONESIMULATOR_FRAMEWORK_DIR}/${TARGET_NAME}" "${IPHONE_FRAMEWORK_DIR}/${TARGET_NAME}"
# fi


# 合并framew，输出xcframework
PATH_XCFramework="${UNIVERSAL_OUTPUT_FOLDER}/${TARGET_NAME}.xcframework"
xcodebuild -create-xcframework -framework "${IPHONE_FRAMEWORK_DIR}" -framework "${IPHONESIMULATOR_FRAMEWORK_DIR}" -output "${PATH_XCFramework}"

# 如果是swift工程，还要拷贝.swiftmodule下的文件
# SIMULATOR_SWIFT_MODULE_DIR="${IPHONESIMULATOR_FRAMEWORK_DIR}/Modules/${TARGET_NAME}.swiftModule"
# if [ -d "$SIMULATOR_SWIFT_MODULE_DIR" ]
# then
# 	cp -R "${SIMULATOR_SWIFT_MODULE_DIR}" "${UNIVERSAL_OUTPUT_FOLDER}/${TARGET_NAME}.framework/Modules"
# fi

# change_Framew_header_Swift_File_Content "${UNIVERSAL_OUTPUT_FOLDER}/${TARGET_NAME}.framework/Headers/${TARGET_NAME}-Swift.h"

echo "================== lipo success ===================="

#判断build文件夹是否存在，存在则删除
if [ -d "${SRCROOT}/build" ]
then
rm -rf "${SRCROOT}/build"
fi
rm -rf "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator" "${BUILD_DIR}/${CONFIGURATION}-iphoneos"
#打开合并后的文件夹
open "${UNIVERSAL_OUTPUT_FOLDER}"
